﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Transcend.Utility
{
    /// <summary>
    /// 文件夹帮助
    /// </summary>
    public class IOHelper
    {
        /// <summary>
        /// 将末级文件夹对应的FromFileId更改为FileId
        /// </summary>
        /// <param name="root"></param>
        /// <param name="files"></param>
        public static void changeFileDir(string root, List<FileRelation> files)
        {
            var dirs = System.IO.Directory.GetDirectories(root);
            foreach (var dir in dirs)
            {
                var dirInfo = new System.IO.DirectoryInfo(dir);
                string dirName = dirInfo.Name;
                var subDir = System.IO.Directory.GetDirectories(dir);
                if (subDir.Length == 0)
                {
                    //没有子目录
                    var file = files.FirstOrDefault(it => it.FromFileId.ToString().Equals(dirName, StringComparison.OrdinalIgnoreCase));
                    if (file != null)
                    {
                        string newDir = System.IO.Path.Combine(root, file.FileId.ToString());
                        System.IO.Directory.Move(dir, newDir);                        
                        continue;
                    }
                }
                else
                {
                    changeFileDir(dir, files);
                }
            }
        }
    }

    public class FileRelation
    {
        public string FileId { get; set; }

        public decimal FromFileId { get; set; }
    }
}
